package com.fufufu.mail.product.service.impl;

import com.fufufu.mail.product.dao.AttrAttrgroupRelationDao;
import com.fufufu.mail.product.entity.AttrAttrgroupRelationEntity;
import com.fufufu.mail.product.entity.AttrEntity;
import com.fufufu.mail.product.service.AttrService;
import com.fufufu.mail.product.vo.AttrGroupAttrRelationVo;
import com.fufufu.mail.product.vo.AttrGroupWithBaseAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.product.dao.AttrGroupDao;
import com.fufufu.mail.product.entity.AttrGroupEntity;
import com.fufufu.mail.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Resource
    private AttrService attrService;


    @Override
    public void deleteRelation(List<AttrGroupAttrRelationVo> relations) {
        // 需要操作数据库了，将前端接收的VO转换成entity
        List<AttrAttrgroupRelationEntity> relationEntities = relations.stream().map((relation) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(relation, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteBatchRelation(relationEntities);

    }

    /**
     * 通过类别id查询其所拥有的属性组
     */
    @Override
    public List<AttrGroupWithBaseAttrVo> getGroupsByCatelogId(Long catelogId) {
        // 先查出所有的属性组
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // 没办法只能循环查了，
        List<AttrGroupWithBaseAttrVo> vos = attrGroupEntities.stream().map((entity) -> {
            AttrGroupWithBaseAttrVo attrGroupWithBaseAttrVo = new AttrGroupWithBaseAttrVo();
            BeanUtils.copyProperties(entity, attrGroupWithBaseAttrVo);
            List<AttrEntity> attrs = attrService.getAttrByAttrgroupId(entity.getAttrGroupId());
            attrGroupWithBaseAttrVo.setAttrs(attrs);
            return attrGroupWithBaseAttrVo;
        }).collect(Collectors.toList());
        return vos;

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        // 有个关键字key的查询，查询出和这个关键字有关的组id或组名
        String key = (String) params.get("key");
        // 这个wrapper用的是装饰者模式的设计模式
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        // 模糊查询
        if (StringUtils.hasText(key)) {
            wrapper.and((wrapper1) -> wrapper1.eq("attr_group_id", key)
                    .or()
                    // 这个like其实是%%
                    .like("attr_group_name", key));
        }
        // 查询某个三级类别
        if (catalogId != 0) {
            // 这一句相当于WHERE catelog_id = catalogId
            wrapper.eq("catelog_id", catalogId)
                    // 这一句相当于 SELETC ...
                    .select("attr_group_id", "attr_group_name", "sort", "descript", "icon", "catelog_id");
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

}